草庐IT

TypeScript 函数

全部标签

javascript - TypeScript 处理接口(interface)和类中多余属性的方式不同

我最近在TypeScript中偶然发现了这种奇怪的(imo)行为。在编译期间,只有当预期变量的类型是接口(interface)且接口(interface)没有必填字段时,它才会提示过多的属性。链接到TypeScriptPlayground#1:http://goo.gl/rnsLjdinterfaceIAnimal{name?:string;}classAnimalimplementsIAnimal{}varx:IAnimal={bar:true};//Objectliteralmayonlyspecifyknownproperties,and'bar'doesnotexistinty

javascript - 使用类将 JSON 对象转换为 typescript 类型变量的正确方法?

这是我正在尝试做的一个更简单的例子:exportclassPerson{id:Number;name:String;}exportclassPersonForm{//Thisline:default:Person={name:"Guy"};//Givesthefollowingerror://Error:(25,5)TS2322:Type'{name:string;}'isnotassignabletotype'Person'.//Property'id'ismissingintype'{name:string;}'.//Itried{name:"Guy"}butitgivesthes

javascript - 在 typescript 中获取函数名称

我正在寻找一种方法来获取传入参数的函数名称console.clear();classA{test(){}testCall(fnc:Function){console.log(fnc.name);//iwantitdisplaytestherenotemptyconsole.log(fnc);}}vara=newA();a.testCall(a.test);你可以在jsbin中查看http://jsbin.com/loluhu/edit?js,console 最佳答案 我发现这是typescript中的一个错误你可以在这里找到解决方案

javascript - 在函数中使用 let 而不是 var 的优点

这个问题在这里已经有了答案:Whatisthedifferencebetween"let"and"var"?(39个答案)关闭6年前。假设我有一段这样的代码:constnumber=3;functionfooFunction(){letnumberTwo=5;varanswer=number+numberTwo;returnanswer;}finalAnswer=fooFunction();console.log(finalAnswer);假设一个兼容ES2015的浏览器,使用上述代码的优点/缺点是什么,超过:constnumber=3;functionfooFunction(){va

javascript - 取消 javascript 中 n 个参数的柯里化(Currying)函数

如果f::a->b->c是柯里化(Currying)的,那么uncurry(f)可以定义为:uncurry::(a->b->c)->((a,b)->c)我正在尝试在javascript中实现上述功能。我的以下实现是否正确且足够通用,或者是否有更好的解决方案?constuncurry=f=>{if(typeoff!="function"||f.length==0)returnf;returnfunction(){for(leti=0;ia=>b=>f(a,b);constcurriedSum=curry((num1,num2)=>num1+num2);console.log(currie

javascript - Object.freeze 函数有什么理由吗?

我理解递归深层对象以对其每个子属性执行浅层Object.freeze的意义。卡住函数对象的值有什么意义?由于较高级别的浅卡住,引用已被卡住——是否可以改变函数对象的值本身?例子://LibraryFunction[deepFreezesource](https://github.com/substack/deep-freeze/blob/master/index.js)functiondeepFreeze(o){Object.freeze(o);//shallowfreezethetoplevelObject.getOwnPropertyNames(o).forEach(functio

javascript - fabric.Canvas 不是构造函数

我像这样包含了织物:但是当我这样使用它时:varcanvas;functioninitSketchPad(){canvas=newfabric.Canvas('sketch-pad',{isDrawingMode:true});}我明白了UncaughtTypeError:fabric.Canvasisnotaconstructor 最佳答案 我不确定您的fabric_freedrawing.js文件中有什么,但将您的代码添加到下面的代码片段似乎工作正常。varcanvas;functioninitSketchPad(){canva

javascript - React 组件在其 props 方面必须表现得像纯函数吗?

我正在浏览React文档并阅读这一行(粗体显示他们的):AllReactcomponentsmustactlikepurefunctionswithrespecttotheirprops.这是避免意外行为的严格要求还是设计原则?Facebook提供的“不纯”函数示例是修改其输入的函数。但是React组件似乎可以修改它作为输入接收的Prop(我已经从他们自己的示例中复制了下面的示例)。CodePen:ImpureComponentw/RespecttoProps?JSX代码:varWithdraw=React.createClass({render(){this.props.accoun

javascript - 什么是 Node.JS 的 getHiddenValue 和 setHiddenValue 函数,它们包装了 V8 的 GetPrivate 和 SetPrivate?

由于非常复杂的原因,我正在研究以更好地了解Node.JS的内部结构,并发现了两个用途不明的函数。这些是功能以及如何访问它们。process.binding('util').setHiddenValueprocess.binding('util').getHiddenValue来自theirnativecodedeclarations,很明显它们包装了以下V8函数:v8::Object::SetPrivatev8::Object::GetPrivate我还制作了一个小片段,展示了他们可以做什么。'usestrict';varbinding=process.binding('util');

javascript - TypeScript 可更新和枚举引用

我有一个用JavaScript编写的外部SDK,我正在使用它。这些模块之一,Blob是可更新的,但也公开了一个枚举FooEnum(成员Bar和Baz).在JavaScript中使用此SDK的代码如下:constblobInstance=newSdk.Blob();constfooType=Sdk.Blob.FooEnum.Baz;我现在正在尝试编写一个接口(interface),我可以将此SDK转换到该接口(interface),以提供一些类型安全性。这是我到目前为止所得到的:interfaceBlobInterface{}enumFoo{Bar,Baz}interfaceSdk{Bl